//
//  UDPViewController.m
//  socketDemo
//
//  Created by 吳瀾洲 on 2020/8/3.
//  Copyright © 2020 kingboyrang. All rights reserved.
//

#import "UDPViewController.h"
#import "UdpManager.h"
#import "QMUIKit.h"
#import "NSString+CZExtend.h"
#import "BaseDataUtils.h"
#import "IpUtils.h"
#import "BaseDataPack.h"
#import "SVProgressHUD.h"

@interface UDPViewController ()
@property (nonatomic,strong) UdpManager *udpClient;
@end

@implementation UDPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.udpClient = [[UdpManager alloc] init];
    
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSuccess:) name:kUDPClientSendSuccessNotifice object:nil];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveFailed:) name:kUDPClientSendFailedNotifice object:nil];
    

}

- (void)receiveSuccess:(NSNotification *)notify{
    self.labStatus.text = @"upd 发送成功";
}
- (void)receiveFailed:(NSNotification *)notify{
    self.labStatus.text = @"upd 发送失败";
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



- (IBAction)sendAction:(id)sender {
    if([[self.hostField.text Trim] length] == 0){
        [QMUITips showError:@"主机不为空"];
        return;
    }
    if([[self.portField.text Trim] length] == 0){
        [QMUITips showError:@"端口不为空"];
        return;
    }
    if([[self.macField.text Trim] length] == 0){
        [QMUITips showError:@"发送内容不为空"];
        return;
    }
    
     __weak __typeof(self) weakSelf = self;
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD showWithStatus:@"发送中..."];
    self.udpClient.sendDataBlock = ^(BOOL success, long tag, NSError *error) {
      
        NSLog(@"tag = %ld",tag);
        if (success) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                               weakSelf.labStatus.text = @"upd 发送成功";
                                          [SVProgressHUD showSuccessWithStatus:@"发送数据成功"];
                               //2秒后关闭
                               dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
                               dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                                   //关闭
                                   [SVProgressHUD dismiss];
                               });
                           });
        }else{
            
            dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.labStatus.text = @"upd 发送失败";
                [SVProgressHUD showErrorWithStatus:@"发送数据失败"];
                //2秒后关闭
                dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
                dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                    //关闭
                    [SVProgressHUD dismiss];
                });
            });
        }
    };
    BaseDataPack *dataPack = [[BaseDataPack alloc] initWithMacAddress:[self.macField.text Trim] tag:1];
    if(dataPack.bodyData == nil){
        [QMUITips showError:@"发送内容转换失败"];
        return;
    }
    
    [self.udpClient sendData:dataPack.bodyData port:[[self.portField.text Trim] integerValue] toHost:[self.hostField.text Trim]];
    //[self.udpClient wakeUp:[self.macField.text Trim] host:[self.hostField.text Trim] port:[[self.portField.text Trim] integerValue]];

}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.portField resignFirstResponder];
    [self.hostField resignFirstResponder];
    [self.macField resignFirstResponder];
}



@end
